<template>
  <div class="add_shop">
    <el-form :model="updataForm" :rules="rules" ref="ruleForm" label-width="112px" label-position="right">
      <el-form-item label="店铺名称:" prop="shopName">
        <el-input v-model="updataForm.shopName" placeholder="请输入店铺名称"></el-input>
      </el-form-item>

      <el-form-item label="经营类型:" prop="businessType">
        <el-select v-model="updataForm.businessType" placeholder="请选择经营类型">
          <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="联系人:" prop="username">
        <span style="width: 200px;display: inline-block;">{{updataForm.username}}</span>
        <!-- <el-input v-model="updataForm.username" placeholder="请输入联系人名称"></el-input> -->
      </el-form-item>

      <el-form-item label="手机号:" prop="telephone">
        <span style="width: 200px;display: inline-block;">{{updataForm.telephone}}</span>
      </el-form-item>

      <el-form-item label="店铺logo:" prop="logo">
        <div class="uploadbtn" @click="uploadBtn">
          <input type="file" @change="upload" multiple ref="file" style="display: none;" />
          <img :src="updataForm.logo" v-if="updataForm.logo != null">
          <i class="el-icon-upload" v-if="updataForm.logo == null" />
        </div>
      </el-form-item>

      <el-form-item label="店铺地址:" prop="shopAddress">
        <el-input id='tipinput' v-model="updataForm.shopAddress" style="width: 500px;" placeholder="请输入商家详细地址"></el-input>
        <div id="tip"></div>
        <div id="map-box"></div>
      </el-form-item>
      
      <el-form-item label="店铺经度:" prop="longitude">
        <el-input v-model.number="updataForm.longitude"></el-input>
      </el-form-item>
      
      <el-form-item label="店铺纬度:" prop="latitude">
        <el-input v-model.number="updataForm.latitude"></el-input>
      </el-form-item>

      <el-form-item label="开户行:" prop="bankName">
        <el-input v-model="updataForm.bankName"></el-input>
      </el-form-item>

      <el-form-item label="开户名称:" prop="accountName">
        <el-input v-model="updataForm.accountName"></el-input>
      </el-form-item>

      <el-form-item label="银行账号:" prop="bankAccount">
        <el-input v-model="updataForm.bankAccount"></el-input>
      </el-form-item>

      <el-form-item label="平台分润:" prop="platformDivide">
        <el-input v-model.number="updataForm.platformDivide">
          <template slot="append">%</template>
        </el-input>
      </el-form-item>

      <el-form-item label="车场分润:" prop="parkDivide">
        <el-input v-model.number="updataForm.parkDivide">
          <template slot="append">%</template>
        </el-input>
      </el-form-item>
    </el-form>

    <p style="text-align: center;">
      <el-button @click="cancle">取消</el-button>
      <el-button type="primary" @click="submit('ruleForm')">确认</el-button>
    </p>




  </div>
</template>

<!-- <script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&'24e73473dd841353fbe1cc26535ff473'&plugin=AMap.Autocomplete"></script> -->
<script>
  import VueAMap from 'vue-amap'
  const amapManager = new VueAMap.AMapManager()

  export default {
    props: ['row'],
    data() {
      var bankAccountValid = (rule, value, callback) => {
        const strBin = '10,18,30,35,37,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,58,60,62,65,68,69,84,87,88,94,95,98,99'
        if (!value) {
          return callback(new Error('收款账户不能为空'))
        } else if (!Number.isInteger(+value)) {
          callback(new Error('银行卡号必须全为数字'))
        } else if (value.trim().length < 12 || value.trim().length > 19) {
          callback(new Error('银行卡号长度必须在12到19之间'))
        } else if (strBin.indexOf(value.substring(0, 2)) === -1) {
          callback(new Error('银行卡号开头6位不符合规范'))
        } else {
          callback()
        }
      }
      return {
        updataForm: {
          logo: []
        },
        options: [
          {
            label: '餐饮',
            value: 1
          },
          {
            label: '加油站',
            value: 2
          },
          {
            label: '洗车',
            value: 3
          },
          {
            label: '养车',
            value: 4
          },
          {
            label: '商超',
            value: 5
          },
          {
            label: '其他',
            value: 6
          }
        ],
        mapKeyword: null,
        rules: {
          shopName: { required: true, message: '请输入商户名称', trigger: 'blur' },
          bankName: { required: true, message: '请输入开户银行', trigger: 'blur' },
          accountName: { required: true, message: '请输入开户名称', trigger: 'blur' },
          platformDivide: [
            { required: true, message: '请输入分成比例', trigger: 'blur' },
            { type: 'number', message: '分成比例为数字值', trigger: 'blur'},
            { pattern: /^(0|[1-9]\d?|100)$/, message: '范围在0-100', trigger: 'blur'}
          ],
          parkDivide: [
            { required: true, message: '请输入分成比例', trigger: 'blur' },
            { type: 'number', message: '分成比例为数字值', trigger: 'blur'},
            { pattern: /^(0|[1-9]\d?|100)$/, message: '范围在0-100', trigger: 'blur'}
          ],
          longitude: { required: true, message: '请输入店铺经度', trigger: 'blur' },
          latitude: { required: true, message: '请输入店铺纬度', trigger: 'blur' },
          username: { required: true, message: '请输入联系人', trigger: 'blur' },
          telephone: { required: true, message: '请输入正确的手机号', trigger: 'blur' },
          businessType: { required: true, message: '请选择经营类型', trigger: 'blur' },
          logo: { required: true, message: '请上传logo', trigger: 'blur' },
          shopAddress: { required: true, message: '请输入商户地址', trigger: 'blur' },
          // bankAccount: { required: true, message: '请输入正确的银行账户', trigger: 'blur' },
          bankAccount: [{ required: true, trigger: 'blur', validator: bankAccountValid }]
        }
      }
    },

    mounted() {
      this.getUpdataForm(this.row.id);
      let _this = this;
      let position = [114.614693, 38.044273];
      AMap.plugin('AMap.Autocomplete', function(){
        // 实例化Autocomplete
        var autoOptions = {
          city: '全国',
          input: "tipinput",
          output: "tip"
        }
        var autoComplete= new AMap.Autocomplete(autoOptions)
        autoComplete.search(_this.mapKeyword, function(status, result) {
          // console.log(111,status,222,result)
        })
        AMap.event.addListener(autoComplete, "select", select);
        function select(e) {
          _this.updataForm.shopAddress = e.poi.district + e.poi.address;
          _this.updataForm.longitude = e.poi.location.lng
          _this.updataForm.latitude = e.poi.location.lat
          position = [e.poi.location.lng,e.poi.location.lat]
          var marker = new AMap.Marker({
            position: [e.poi.location.lng,e.poi.location.lat]
          });
          map.setCenter([e.poi.location.lng,e.poi.location.lat]);
          map.add(marker);
        }
      })
      var map = new AMap.Map('map-box',{
        center: position,
        zoom:13
      });
      var clickHandler = function(e) {
        alert('您在[ '+e.lnglat.getLng()+','+e.lnglat.getLat()+' ]的位置点击了地图！');
      };









    },

    methods: {
      getUpdataForm(id) {
        this.$axios({
          method: 'get',
          url: '/shop/shop-info/getShopById',
          params: {shopId: id}
        }).then((res) => {
          console.log(res)
          if(res.code == 200) {
            this.updataForm = res.data;
          }
        })
      },

      submit(formName) {
         this.$refs[formName].validate((valid) => {
           if(valid) {
            this.$axios({
              method: 'post',
              url: '/shop/shop-info/update',
              data: this.$qs.stringify(this.updataForm)
            }).then((res) => {
              if(res.code == 200) {
                this.$message.success('修改商户成功');
                this.$emit('closeDialog');
                this.$emit('refresh')
              }
            })
           }
         })
      },

      cancle() {
        this.$emit('closeDialog',false);
      },

      uploadBtn(){
        this.$refs.file.dispatchEvent(new MouseEvent('click'))
      },

      upload(e) {
        let files = e.target.files
        this.$nextTick(() => {
            // this.option.img = URL.createObjectURL(files[0]);
            // this.fileinfo = files[0].name;
            // this.dialogVisible = true
        })
        for (let i = 0; i < files.length; i++) {
          const isImg = files[i].type.indexOf('image') > -1
          const isLimit = files[i].size / 1024 / 1024 < 10

          if (!isImg) {
            this.$message.error('请上传图片格式!')
            return false
          }
          if (!isLimit) {
            this.$message.error('大小不能超过 10MB!')
            return false
          }
          let formData = new FormData()
          formData.append('files', files[i])

          var image = { url: null, percent: 0 }
          // this.imageList.push(image)

          this.uploadAxios(formData, image)
        }
      },

      uploadAxios(formData, image){
        this.$axios({
          url: 'https://file.zmzncs.com/upload/upload-batch',
          method: 'post',
          data: formData,
          timeout: 1000 * 60, // 请求超时时间
          onUploadProgress: (progress)=> {
            image.percent = Math.round(progress.loaded / progress.total * 100)
          }
        }).then((res) => {
          if (res.code == 200) {
            this.updataForm.logo = res.data[0].url;
            image.percent = 100
            image.url = res.data[0].url
            // this.imgs.push(image.url)
            //  组件返回值
            // var urlList = []
            // this.imgs = [];
            // this.imageList.forEach((image)=> {
            //   this.imgs.push(image.url)
            // })
            // var urls = urlList.toString()
            // this.$emit('input', urls)
          }
        }).catch(err => {})
      },

    }
  }
</script>

<style scoped>
  .el-form-item {
    display: inline-block;
  }

  .el-input {
    width: 200px;
  }

  .address--box {
    height: 240px;
    width: 500px;
    border: 1px solid red;
  }

  #map-box {
    height: 300px;
    margin-top: 10px;
    border: 1px solid #ccc;
  }

  #tip {
    width: 100%;
    height: 240px;
    border: 1px solid #eee;
    overflow: auto;
    position: absolute;
    z-index: 1000;
    background-color: #fff;
  }

  .uploadbtn {
    border: 1px solid #aaa;
    height: 100px;
    width: 100px;
    position: relative;
  }

  .uploadbtn img {
    max-width: 100%;
    max-height: 100%;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
  }

  .el-icon-upload {
    font-size: 50px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
  }






</style>
